// pages/xianwangdian/xianwangdian.js


const app = getApp();
// import { hexMD5 } from "../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: Number, // 起始页码
    videlist: [],
    openid: '',
    jiami: '',
    OPEN_ID: '',
    session_key: '',

    userlatitude: '', //纬度
    userlongitude: '', //经度

  },



  getvidelist(n = '') {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })



    console.log(this.data.jiami);
    console.log(this.data.openid);
    console.log(this.data.OPEN_ID);
    console.log(this.data.session_key);


    wx.request({
      url: app.globalData.interface+'/squareDance/API_5211_xianEmployeeMessage',
      method: 'POST',
      data: {
        KeyWord: n,
        currentPage: '', //当前页   非必填
        CITYCODE:'shangluo',
        DIMENSION:this.data.userlatitude,
        LONGITUDE:this.data.userlongitude,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res);
        console.log('res', res.data.result);
        wx.hideLoading()
        if (res.data.result == 'success' && res.data.data.length != 0) {
          console.log('翻页', res.data.data[0].page.totalPage);

          const copypage = JSON.stringify(res.data.data[0].page.totalPage)
          const totalPage = JSON.parse(copypage)
          console.log('totalPage', totalPage);

          console.log(res.data.data, );



          const liscop = res.data.data.map((item) => {
            console.log(this.data.userlatitude);
            console.log(this.data.userlongitude);
            console.log(item.DIMENSION);
            console.log(item.LONGITUDE);
            return {
              ...item,
              DISTANCE: this.getDistance(this.data.userlatitude, this.data.userlongitude, item.DIMENSION, item.LONGITUDE)
            }


          })

          console.log(liscop);


          this.setData({
            videlist: liscop,
            totalPage: totalPage
          })



          console.log('最大页码数', this.data.totalPage);

          // this.findXy()



        } else {
          wx.showModal({
            title: "暂无该条数据",
            content: '请重新输入关键词',
            showCancel: false,
          });
        }
      },
      fail: (res => {
        wx.hideLoading()
      })
    })


  },


  clear() {
    console.log('最大页码', this.data.totalPage);
    this.setData({
      page: 1,
      videlist: []
    })
    setTimeout(() => {
      this.getvidelist()
    }, 500);
  },
  onSearch(event) {
    const n = event.detail
    this.getvidelist(n)
  },
  onchange(event) {
    const value = event.detail
    if (value == '') {
      console.log('8585', this.data.page);
      this.setData({
        page: 1, // 起始页码
        videlist: [],
      })
      setTimeout(() => {
        this.getvidelist()
      }, 500);

    }
  },









  Rad: function (d) { //根据经纬度判断距离
    return d * Math.PI / 180.0;
  },

  getDistance: function (lat1, lng1, lat2, lng2) {
    // lat1用户的纬度
    // lng1用户的经度
    // lat2商家的纬度
    // lng2商家的经度
    var radLat1 = this.Rad(lat1);
    var radLat2 = this.Rad(lat2);
    var a = radLat1 - radLat2;
    var b = this.Rad(lng1) - this.Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    // s = s.toFixed(2) + '公里' //保留两位小数
    s = s.toFixed(2) //保留两位小数
    console.log('经纬度计算的距离:' + s)
    return s
  },




  quditu(e) {

    console.log(e);
    console.log(e.currentTarget.dataset.item.DIMENSION);
    console.log(e.currentTarget.dataset.item.LONGITUDE);
    console.log(e.currentTarget.dataset.item.EMPLOYEE_ADD);
    console.log(e.currentTarget.dataset.item.L4name);

    if (e.currentTarget.dataset.item.DIMENSION != '') {

      wx.openLocation({
        // longitude: Number(this.data.longitude),
        // latitude: Number(this.data.latitude),
        longitude: Number(e.currentTarget.dataset.item.LONGITUDE),
        latitude: Number(e.currentTarget.dataset.item.DIMENSION),
        name: e.currentTarget.dataset.item.L4name,
        address: e.currentTarget.dataset.item.EMPLOYEE_ADD
      })
    }








  },










  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.findXy()
    let that = this
    that.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res);
        console.log(res.latitude);
        console.log(res.longitude);

        that.setData({
          userlatitude: res.latitude,
          userlongitude: res.longitude
        })


        // _this.getDistance(res.latitude, res.longitude, item.DIMENSION, item.LONGITUDE)
      }




    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //查询用户与商家的距离




    console.log(this.data.jiami);

    this.setData({
      page: 1, // 起始页码
    })

    setTimeout(() => {
      this.getvidelist()
    }, 500);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let totalP = this.data.totalPage //最大页码数
    const that = this
    let page = that.data.page + 1 // 当前页码+1，获取下一页的数据
    console.log('page', page);

    if (page <= totalP && totalP > 1) {
      that.setData({
        page: page,
      })
      wx.request({
        url: app.globalData.interface+'/squareDance/API_5211_xianEmployeeMessage',
        method: 'POST',
        data: {

          currentPage: page, //当前页   非必填
          CITYCODE:'shangluo',
          DIMENSION:this.data.userlatitude,
          LONGITUDE:this.data.userlongitude,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);

          if (res.data.result == 'success') {


            const liscop = res.data.data.map((item) => {
              console.log(this.data.userlatitude);
              console.log(this.data.userlongitude);
              console.log(item.DIMENSION);
              console.log(item.LONGITUDE);
              return {
                ...item,
                DISTANCE: this.getDistance(this.data.userlatitude, this.data.userlongitude, item.DIMENSION, item.LONGITUDE)
              }


            })


            console.log(liscop);

            that.setData({
              videlist: [...that.data.videlist, ...liscop], //es6中的在旧数据中加其他数据,
            })

            // that.findXy()

            wx.hideLoading()
            console.log('最大页码数', this.data.totalPage);

          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })
      console.log("56565656", this.data.page);

    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'error',
        duration: 1500,
      })
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})